package com.exampleyyy.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exampleyyy.demoyyy.ServiceJieKou.EmployeeServiceJieKou;
import com.exampleyyy.demoyyy.ShiTi.Employee;
import com.exampleyyy.demoyyy.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")  // 捕捉主路由
public class EmployeeContoller {
    @Autowired
    private EmployeeServiceJieKou fuwujiekou;
    @PostMapping("/login")
    public  R<Object>  login(HttpServletRequest request, @RequestBody Employee emp ){
        System.out.println("前端传来的用户名是"+emp.getUsername());
        System.out.println("前端传来的密码是"+emp.getPassword());
        System.out.println("前端传来的密码转为字节类型是"+emp.getPassword().getBytes());
        String  miwen = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
        System.out.println("加密后的密文是"+miwen);
        LambdaQueryWrapper<Employee>  chaxunduixiang = new LambdaQueryWrapper();
        chaxunduixiang.eq(Employee::getUsername,emp.getUsername());
        Employee TableResult = fuwujiekou.getOne(chaxunduixiang);//相当于执行SQL select * from employee where username="admin";
        System.out.println("数据库查询结果是"+TableResult);
        if(TableResult == null){
            return R.error(0,"用户名错误",null);
        }
        if (TableResult.getPassword().equals(miwen)){
            request.getSession().setAttribute("员工信息id",TableResult.getId());
            return R.success(1,"登录成功",TableResult);
        }
        else{
            return R.error(0,"密码错误",null);
        }
    }
    @PostMapping("/logout")
    public R<Object> logout(HttpServletRequest request){
        request.getSession().removeAttribute("员工信息ID");
        return R.success(1,"退出成功",null);
    }
    @PostMapping("")
    public R<Object> addyuangong(HttpServletRequest request, @RequestBody Employee emp){
        System.out.println("前端刚来的时候的emp"+emp);
        String MoRenmiwen = DigestUtils.md5DigestAsHex(/*emp.getPassword()*/"123456".getBytes());
        emp.setPassword(MoRenmiwen);
        System.out.println("emp添加密文之后的emp"+emp);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        System.out.println("添加时间之后的emp"+emp);
        Long empid = (Long)request.getSession().getAttribute("员工信息id");
        emp.setUpdateUser(empid);
        emp.setCreateUser(empid);
        System.out.println("添加创建员工之后的emp"+emp);
        try{
            fuwujiekou.save(emp);
            return R.success(1,"添加员工成功",null);
        }catch (Exception eee){
            System.out.println(eee);
            return R.error(0,"添加员工失败，用户名重复",null);
        }
    }
    @GetMapping("/page")
    public R<Object> chaxunyuangong(int page,int pageSize, String name){
        LambdaQueryWrapper<Employee>  chaxunduixiang = new LambdaQueryWrapper();
        Page fenyeduixiang = new Page(page,pageSize);
        if(name!=null){
            chaxunduixiang.like(true,Employee::getName,name);
            fuwujiekou.page(fenyeduixiang,chaxunduixiang);
            return R.success(1,"搜索成功",fenyeduixiang);
        };
        try{
            fuwujiekou.page(fenyeduixiang,chaxunduixiang);
        }catch (Exception yichang){
            System.out.println("查询数据库错误，检查代码118彳亍");
            R.error(0,"查询员工失败",null);
        }
        return R.success(1,"查询员工成功",fenyeduixiang);
    }
    @PutMapping("")
    public R<Object> xiugaiyuangong(HttpServletRequest request,@RequestBody Employee emp){
        System.out.println("打印前端请求体给了emp："+emp);
        emp.setUpdateTime(LocalDateTime.now());
        Long sessionid = (Long)request.getSession().getAttribute("员工信息id");
        emp.setUpdateUser(sessionid);
        System.out.println("再次打印emp对比之前的emp："+emp);
        fuwujiekou.updateById(emp);
        return R.success(1,"修改测试",null);
    }
    @GetMapping("/{id}")
    public R<Object> binajiyuangong(@PathVariable Long id){
        Employee emp = fuwujiekou.getById(id);
        if(emp!= null){
            return R.success(1,"查询成功",emp);
        }else{
            return R.error(0,"查无此人",null);
        }
    }
}